package com.shanghaionstar.nav.adapter;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.mag.poi.FavoriteEntity;
import com.onstar.cn.map.common.LocationType;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.R;
import com.shanghaionstar.iview.ILoginListener;
import com.shanghaionstar.nav.presenter.NavMainPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.LoadCityList;
import com.shanghaionstar.utils.LoginUtil;

import java.util.List;

/**
 * @author jun chen
 * @ClassName: NavPoiSearchFavAdapter
 * @date 2016年3月4日 下午5:59:27
 * @Description: 收藏历史适配器
 */
public class NavPoiSearchFavAdapter extends CommonAdapter<FavoriteEntity> implements ILoginListener {
    private boolean reCount = false;
    private AbstractMapActivity mapActivity;
    private NavMainPresenter navMainPresenter;

    public NavPoiSearchFavAdapter(List<FavoriteEntity> favoriteEntities, AbstractMapActivity mapActivity, NavMainPresenter navMainPresenter) {
        super(favoriteEntities, mapActivity);
        this.mapActivity = mapActivity;
        this.datas = favoriteEntities;
        this.navMainPresenter = navMainPresenter;
    }

    public void setList(List<FavoriteEntity> favoriteEntities) {
        this.datas = favoriteEntities;
    }

    @Override
    public void getView(final int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
//        if(datas==null || datas.size() == 0)
        String title = datas.get(position).getPoiNickname();
        if (CommonUtil.isNull(title))
            title = datas.get(position).getPoiName();
        if (CommonUtil.isNull(title))
            title = datas.get(position).getPoiAddress();
        if (!CommonUtil.isNull(title)) {
            holder.text_vendor_name.setText(title);
        } else {
            holder.text_vendor_name.setText("----");
        }
        String address = datas.get(position).getPoiAddress();
        if (!CommonUtil.isNull(address)) {
            holder.text_vendor_addres.setText(address);
        } else {
            holder.text_vendor_addres.setText("----");
        }
        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
            holder.btn_sendtbt.setTextSize(12f);
        } else {
            holder.btn_sendtbt.setTextSize(14f);
        }

        holder.btn_sendtbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("poiSearchFav", datas.get(position));
                new LoginUtil().loginAndActionCallBack(context, RxConstants.NAV_SEARCH_FAV_PAGE, NavPoiSearchFavAdapter.this, bundle);
            }
        });
        //长按事件
        holder.viewoitem.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PoiItemInfo info = new PoiItemInfo();
                FavoriteEntity favoriteEntity = datas.get(position);
                info.setTitle(favoriteEntity.getPoiName());
                info.setCityName(favoriteEntity.getCityCode());
                info.setCityCode(favoriteEntity.getPoiCityCode());
                info.setId(favoriteEntity.getFavoriteDestinationID());
                info.setLatitude(favoriteEntity.getPoiCoordinate().getLatitude());
                info.setLongitude(favoriteEntity.getPoiCoordinate().getLongitude());
                info.setAddress(favoriteEntity.getPoiAddress());
                info.setTelphone(favoriteEntity.getPoiPhoneNumber());
                info.setTypeDes(favoriteEntity.getPoiCatetory());

                String city = info.getCityName();
                if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
                    LoadCityList cityList = new LoadCityList(mapActivity);
                    city = cityList.findByZhCity(info.getCityName());
                }
                info.setCityName(city);
                navMainPresenter.deleteFav(info, position);
                return true;
            }
        });

        //点击事件
        holder.viewoitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FavoriteEntity favoriteEntity = datas.get(position);
//                BusinessUtil.sendReportByClickType(mapActivity,
//                        FunctionNameFindId.Searchpoi_clickfavorite.getStringValue());
                PoiItemInfo info = new PoiItemInfo();
                info.setTitle(favoriteEntity.getPoiName());
                info.setCityName(favoriteEntity.getCityCode());
                info.setCityCode(favoriteEntity.getPoiCityCode());
                info.setId(favoriteEntity.getFavoriteDestinationID());
                info.setLatitude(favoriteEntity.getPoiCoordinate().getLatitude());
                info.setLongitude(favoriteEntity.getPoiCoordinate().getLongitude());
                info.setAddress(favoriteEntity.getPoiAddress());
                info.setTelphone(favoriteEntity.getPoiPhoneNumber());
                info.setTypeDes(favoriteEntity.getPoiCatetory());

                String city = info.getCityName();
                if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
                    LoadCityList cityList = new LoadCityList(mapActivity);
                    city = cityList.findByZhCity(info.getCityName());
                }
                info.setCityName(city);
                mapActivity.showPoiToMap(info, LocationType.POI);
            }
        });
    }

    /**
     * tbc
     *
     * @return
     */
//    private void saveFavoritePoi(String poiItemId, boolean isFavoritePoi) {
//        Map<String, Bundle> map = PoiLocationInfoInstance.getInstance().getPoiBundle();
//        if (map.containsKey(Constants.FAVORITEPOI)) {
//            map.get(Constants.FAVORITEPOI).putBoolean(poiItemId, isFavoritePoi);
//        } else {
//            Bundle bundle = new Bundle();
//            bundle.putBoolean(poiItemId, isFavoritePoi);
//            map.put(Constants.FAVORITEPOI, bundle);
//        }
//        PoiLocationInfoInstance.getInstance().setPoiBundle(map);
//    }
    @Override
    public int layoutId() {
        return R.layout.nav_history_fav_listview_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(R.id.viewoitem)
        RelativeLayout viewoitem;
        @FindViewById(R.id.text_vendor_name)
        TextView text_vendor_name;
        @FindViewById(R.id.text_vendor_addres)
        TextView text_vendor_addres;
        @FindViewById(R.id.btn_sendtbt)
        RippleCustomBtn btn_sendtbt;
    }

    @Override
    public void onLogin(Bundle bundle, String resultCode) {
        if (bundle != null) {
            if (RxConstants.ACTION_SUCCESS.equals(resultCode)) {
//                BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Searchpoi_history_navigation.getStringValue());
                FavoriteEntity favoriteEntity = (FavoriteEntity) bundle.getSerializable("poiSearchFav");
                if (favoriteEntity == null) return;
                PoiItemInfo poiItemInfo = new PoiItemInfo();
                poiItemInfo.setTitle(favoriteEntity.getPoiName());
                poiItemInfo.setAddress(favoriteEntity.getPoiAddress());
                poiItemInfo.setLatitude(favoriteEntity.getPoiCoordinate().getLatitude());
                poiItemInfo.setLongitude(favoriteEntity.getPoiCoordinate().getLongitude());
                poiItemInfo.setCityName(favoriteEntity.getPoiCityCode());
                poiItemInfo.setTelphone(favoriteEntity.getPoiPhoneNumber());
                navMainPresenter.sendTbtOrOdd(poiItemInfo);
            }
        }
    }
}
